package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjUserDynamic;
import cn.stylefeng.guns.modular.mapper.IdjUserCollectMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserDynamicMapper;
import cn.stylefeng.guns.modular.service.IIdjUserDynamicService;
import cn.stylefeng.roses.core.util.ToolUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 用户动态 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-30
 */
@Service
public class IdjUserDynamicServiceImpl extends ServiceImpl<IdjUserDynamicMapper, IdjUserDynamic> implements IIdjUserDynamicService {

	@Autowired
	private IdjUserDynamicMapper mapper;
	
	@Autowired
	private IdjUserCollectMapper collectMapper;
	
	@Override
	public LayuiPageInfo getList(Page<IdjUserDynamic> page, String keyword, String beginTime, String endTime,
			Integer indexShow, Integer isAudit) {
		Page<Map<String,Object>> list = mapper.getList(page, keyword, beginTime, endTime, indexShow, isAudit);
		list.getRecords().forEach(f -> {
			if(ToolUtil.isNotEmpty(f.get("dynamic_image"))){
				String str = f.get("dynamic_image").toString();
				String[] split = str.split(",");
				List<String> lists = new ArrayList<String>();
				for (int i = 0; i < split.length; i++) {
					if(split[i].indexOf(".jpg") > 0 || split[i].indexOf(".png") > 0){
						f.put("lx", 1);
						lists.add(split[i]);
					}
					if(split[i].indexOf(".mp3") > 0 || split[i].indexOf(".MP3") > 0){
						f.put("lx", 2);
						lists.add(split[i]);
					}
					if(split[i].indexOf(".MP4") > 0 || split[i].indexOf(".mp4") > 0){
						f.put("lx", 3);
						lists.add(split[i]);
					}
				}
				f.put("imglist", lists);
			}
		});
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long id) {
		IdjUserDynamic dynamic = mapper.selectByKeyId(id);
		if(dynamic != null){
			if(ToolUtil.isNotEmpty(dynamic.getDynamicImage())){
				String[] split = dynamic.getDynamicImage().split(",");
				List<String> lists = new ArrayList<String>();
				for (int i = 0; i < split.length; i++) {
					if(split[i].indexOf(".jpg") > 0 || split[i].indexOf(".png") > 0){
						dynamic.setLx(1);
						lists.add(split[i]);
					}
					if(split[i].indexOf(".mp3") > 0 || split[i].indexOf(".MP3") > 0){
						dynamic.setLx(2);
						lists.add(split[i]);
					}
					if(split[i].indexOf(".MP4") > 0 || split[i].indexOf(".mp4") > 0){
						dynamic.setLx(3);
						lists.add(split[i]);
					}
				}
				dynamic.setImglist(lists);
			}else{
				dynamic.setLx(0);
			}
			if(dynamic.getIsAudit()== 1){
				dynamic.setIsAuditName("已通过");
			}else if(dynamic.getIsAudit()== 2){
				dynamic.setIsAuditName("已拒绝");
			}
			if(dynamic.getIndexShow()== 1){
				dynamic.setIndexShowName("设置精选");
			}else if(dynamic.getIndexShow()== 2){
				dynamic.setIndexShowName("取消精选");
			}
		}
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("dynamic", dynamic);
        return modelAndView;
	}

	@Override
	public int setIsAudit(IdjUserDynamic dynamic) {
		return mapper.setIsAudit(dynamic);
	}

	@Override
	public int delete(Long id) {
		int i = mapper.delete(id);
		collectMapper.delete(id, 2);
		return i;
	}

}
